package com.linian.pushforcampus.service.Impl;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.linian.pushforcampus.service.CacheService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;


@Service
@Slf4j
public class CacheServiceImpl implements CacheService {
    private Cache<String, String> cache;
    @Override
    public void createCache(String key, String value) {
        cache.put(key, value);
    }

    @Override
    public String getCache(String key) {
        String token = cache.getIfPresent(key);
        return token;
    }

    public void initCache(long cacheExpirationTime) {
        cache = CacheBuilder.newBuilder()
                .expireAfterWrite(cacheExpirationTime, TimeUnit.SECONDS)
                .build();
    }

    @Override
    public void updateCache(String key, String value) {
        cache.put(key, value);
    }

    @Override
    public void closeCache() {
        cache.cleanUp();
    }


}
